﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EnvironmentalMonitor.Support.Hibernate.Manage;
using EnvironmentalMonitor.Support.Hibernate;
using EnvironmentalMonitor.Support.Module.Manage;
using EnvironmentalMonitor.Support.Module;

namespace EnvironmentalMonitor.Support.Business.Manage
{
    /// <summary>
    /// 岗位和用户关系业务类
    /// </summary>
    public class PostUserBusiness
    {
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页行数</param>
        /// <param name="total">总数</param>
        /// <returns>岗位和用户关系集合</returns>
        public List<PostUser> Query(int page, int rows, ref int total)
        {
            List<PostUser> results = new List<PostUser>();

            PostUserHibernate hibernate = new PostUserHibernate();
            results = hibernate.Query(page, rows, ref total);

            return results;
        }

        /// <summary>
        /// 通过用户查询
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="total">总数</param>
        /// <returns>岗位和用户关系</returns>
        public List<PostUser> QueryByUser(User user, ref int total)
        {
            List<PostUser> results = new List<PostUser>();

            UserHibernate userHibernate = new UserHibernate();
            user = userHibernate.QueryByGuid(user.Guid);

            PostHibernate postHibernate = new PostHibernate();
            List<Post> posts = postHibernate.Query(1, int.MaxValue, ref total);

            PostUserHibernate postUserHibernate = new PostUserHibernate();
            List<PostUser> postUsers = postUserHibernate.QueryByUser(user.Guid);

            for (int i = 0; i < posts.Count; i++)
            {
                PostUser postUser = new PostUser();
                postUser.PostId = posts[i].Guid;
                postUser.UserId = user.Guid;
                postUser.PostName = posts[i].Name;
                postUser.UserName = user.Name;

                for (int j = 0; j < postUsers.Count; j++)
                {
                    if (posts[i].Guid == postUsers[j].PostId)
                    {
                        postUser = postUsers[j];
                        break;
                    }
                }
                results.Add(postUser);
            }
            total = results.Count;

            return results;
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Insert(PostUser value)
        {
            bool result = false;

            PostUserHibernate hibernate = new PostUserHibernate();
            result = hibernate.Insert(value);

            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="guid">主键</param>
        /// <returns>结果</returns>
        public bool Delete(string guid)
        {
            bool result = false;

            PostUserHibernate hibernate = new PostUserHibernate();
            result = hibernate.Delete(guid);

            return result;
        }
    }
}
